package com.zjzaki.thingsboard.adapter;


import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.zjzaki.thingsboard.R;

import java.util.List;

/**
 * 蓝牙列表适配器
 */
public class ListViewBluetoothAdapter extends BaseAdapter {

    private List<BluetoothDevice> deviceBluetoothList;
    private List<Short> listRssi;
    private LayoutInflater inflater;


    /**
     * 构造函数
     * @param context
     * @param deviceBluetoothList
     * @param listRssi
     */
    public ListViewBluetoothAdapter(Context context, List<BluetoothDevice> deviceBluetoothList, List<Short> listRssi) {
        inflater = LayoutInflater.from(context);
        this.deviceBluetoothList = deviceBluetoothList;
        this.listRssi = listRssi;
    }

    /**
     * 得到数据源长度
     *
     * @return 数据源长度
     */
    @Override
    public int getCount() {
        return deviceBluetoothList.size();
    }

    /**
     * 每一行绑定数据源
     *
     * @param position
     * @return
     */
    @Override
    public Object getItem(int position) {
        return deviceBluetoothList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 获取每一行的view
     *
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @SuppressLint("MissingPermission")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ListViewBluetoothAdapter.ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ListViewBluetoothAdapter.ViewHolder();
            //xml加载成View
            convertView = inflater.inflate(R.layout.device_bluetooth_view, parent, false);
            //加载里面的元素
            viewHolder.txtName = convertView.findViewById(R.id.txtName);
            viewHolder.txtMac = convertView.findViewById(R.id.txtMac);
            viewHolder.txtRssi = convertView.findViewById(R.id.txtRssi);
            viewHolder.txtState = convertView.findViewById(R.id.txtState);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ListViewBluetoothAdapter.ViewHolder) convertView.getTag();
        }

        BluetoothDevice device = deviceBluetoothList.get(position);

        viewHolder.txtName.setText(device.getName());
        viewHolder.txtMac.setText(device.getAddress());
        viewHolder.txtRssi.setText(listRssi.get(position) + "");
        if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
            viewHolder.txtState.setText("未匹配");
        } else if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
            viewHolder.txtState.setText("已匹配");
        }
        
        return convertView;
    }

    /**
     * 定义类
     */
    private class ViewHolder {
        private TextView txtName;
        private TextView txtMac;
        private TextView txtRssi;
        private TextView txtState;

    }
}
